import WebHeader from '@/www/components/WebHeader'
import WebFooter from '@/www/components/WebFooter'
import { getQuote, uploadFile, postQuoteOrder } from '@/apiv2/quote'
import { Loading } from 'element-ui';

const CODE = 'SB'
const QUOTE_NAME = '商标';
const MAX_WIDTH = 200
const MAX_LENGTH = 110

export default {
  components: { WebHeader, WebFooter },
  data() {
    return {
      quoteData: undefined,
      quoteValue: {
        category: undefined,
        lamination: undefined,
        style: 1,
        quantity: undefined,
        size: undefined,
        customLength: undefined,
        customWidth: undefined,
      },

      user: undefined,
      customSize: false,
      customLength: undefined,
      customWidth: undefined,
      quote: undefined,
      quoteList: undefined,
      detail: undefined,
      category: undefined,
      lamination: undefined,
      size: undefined,
      quantity: undefined,
      fileList: [],
      maxWidth: 200,
      logic: undefined,
      postData: {
        quantity: undefined,
        detail: undefined,
        amount: undefined,
        fileIds: undefined
      }
    }
  },

  created(){
    const user = JSON.parse(localStorage.getItem('user'))
    this.user = user

    getQuote(CODE).then(res => {
      console.log(res)
      let quoteData = res.config
      this.quoteValue.category = quoteData.cateData.list[0].name
      this.quoteValue.lamination = quoteData.laminationData.list[0].name
      this.quoteValue.size = quoteData.sizeData.list[0].name
      this.quoteValue.quantity = quoteData.stepData.list[0].name
      this.quoteData = quoteData
    })
  },

  methods: {

    getQuote1(){
      let logic = []
      let {quoteData,quoteValue,customSize,postData} = this
      let sqm = undefined
      let length = undefined
      let width = undefined
      if(quoteValue.customLength && quoteValue.customWidth && customSize){
        length = parseFloat(quoteValue.customLength)
        width = parseFloat(quoteValue.customWidth)
      }else if(quoteValue.size){
        length = parseFloat(quoteValue.size.split('*')[0])
        width = parseFloat(quoteValue.size.split('*')[1])
      }
      sqm = parseFloat(length * width / 1000000).toFixed(6)

      console.log('sqm')
      console.log(sqm)

      let rowCount = parseInt(MAX_WIDTH / length)
      let columnCount = parseInt(MAX_WIDTH / width)
      let perCount = parseInt((rowCount + columnCount) / 2)
      console.log('perCount')
      console.log(perCount)

      let detail = ''
      detail += quoteValue.category + '，'
      detail += quoteValue.lamination + '，'
      detail += length + '*' + width + ' mm，'
      detail += '数量：' + quoteValue.quantity

      postData.detail = detail
      postData.quantity = quoteValue.quantity
      postData.code = CODE

      let materialPricePerSquared = 0.0
      quoteData.cateData.list.forEach(item => {
        if(quoteValue.category == item.name){
          materialPricePerSquared += parseFloat(item.price)
          console.log(item)
        }
      })
      quoteData.laminationData.list.forEach(item => {
        if(quoteValue.lamination == item.name){
          materialPricePerSquared += parseFloat(item.price)
          console.log(item)
        }
      })
      console.log('materialPricePerSquared')
      console.log(materialPricePerSquared)

      let quoteList = []
      quoteData.stepData.list.forEach(i => {
        console.log('-------------------------------------------------------------------------------------------------')
        let qty = parseInt(i.name)//阶梯数量

        let perBanCount = perCount //每版数量
        if(qty <= quoteData.param.withinQuantity){
          perBanCount = quoteData.param.fixedSpellCount
        }
        console.log('perBanCount')
        console.log(perBanCount)

        //阶梯总印数
        let printCount = parseInt(qty / perBanCount)
        console.log('printCount')
        console.log(printCount)

        //版费
        let banFee = parseFloat(length) * parseFloat(width) * parseFloat(perBanCount) / parseFloat(1000000) * parseFloat(quoteData.param.feePerSquare) * parseFloat(quoteValue.style)
        console.log('banFee')
        console.log(banFee)

        //印费
        let printFee = parseFloat(printCount) * parseFloat(quoteData.param.feePerPrint) + parseFloat(quoteData.param.start)
        console.log('printFee')
        console.log(printFee)

        let processFee = parseFloat((banFee + printFee))
        console.log('processFee')
        console.log(processFee)

        //材料费
        let materialFee = parseFloat(length) * parseFloat(width) / parseFloat(1000000) * materialPricePerSquared * parseFloat(qty)
        console.log('materialFee')
        console.log(materialFee)
        let total = Math.round(materialFee + processFee)

        if(qty == quoteValue.quantity){
          logic.push("版费 = 尺寸" + length + " * " + width + " * 每版数量" + perBanCount + " / 1000000 * 每平版费" + quoteData.param.feePerSquare)
          logic.push("印费 = 印数" + printCount + " * 每印费用" + quoteData.param.feePerPrint + " + 起点" + quoteData.param.start)
          logic.push("材料 = 尺寸" + length + " * " + width + " / 1000000 * 每平材料费" + materialPricePerSquared + " * 数量" + qty)
          logic.push("总价 = 版费 + 印费 + 材料")
          this.logic = logic
          postData.amount = total
        }

        quoteList.push({
          quantity: qty,
          materialQuantityStepPrice: materialFee,
          processQuantityStepPrice: processFee,
          subtotal: total
        })
        console.log('-------------------------------------------------------------------------------------------------')
      })
      console.log('quoteList')
      console.log(quoteList)
      this.quoteList = quoteList
    },



    getQuote(){
      let logic = []
      let {customLength,customWidth,quote,category,lamination,size,quantity,maxWidth,postData} = this
      let queryText = category + '，' + lamination
      let sqm = undefined
      let length = undefined
      let width = undefined
      if(customLength && customWidth){
        length = parseFloat(customLength)
        width = parseFloat(customWidth)
      }else if(size){
        length = parseFloat(size.split('*')[0])
        width = parseFloat(size.split('*')[1])
      }
      sqm = parseFloat(length * width / 1000000).toFixed(6)

      console.log('sqm')
      console.log(sqm)

      let materialStepPrice = this.getStepPriceFromList(queryText,quote.config.sList)
      console.log('materialStepPrice')
      console.log(materialStepPrice)

      let rowCount = parseInt(maxWidth / parseInt(length))
      let columnCount = parseInt(maxWidth / parseInt(width))
      let perCount = Math.round((rowCount + columnCount) / 2)
      console.log('perCount')
      console.log(perCount)

      let detail = ''
      detail += category + '，'
      detail += length + '*' + width + ' mm，'
      detail += '数量：' + quantity

      postData.detail = detail
      postData.quantity = quantity
      postData.code = CODE

      let quoteList = []
      quote.config.options.stepOptions.forEach(i => {
        console.log('-------------------------------------------------------------------------------------------------')
        let qty = parseInt(i.value)//阶梯数量

        let perBanCount = perCount //每版数量
        if(qty <= quote.config.param.belowCount){
          perBanCount = quote.config.param.fixedCount
        }
        console.log('perBanCount')
        console.log(perBanCount)

        //阶梯材料单价
        let materialUnitPrice = materialStepPrice[i.label]
        console.log('materialUnitPrice')
        console.log(materialUnitPrice)

        //阶梯材料金额
        let materialQuantityStepPrice = parseFloat((materialUnitPrice * qty * sqm).toFixed(2))
        console.log('materialQuantityStepPrice')
        console.log(materialQuantityStepPrice)

        //阶梯总印数
        let printCount = Math.round(qty / perBanCount)

        console.log('printCount')
        console.log(printCount)

        //版费
        let banPrice = parseFloat(length) * parseFloat(width) * parseFloat(perBanCount) / parseFloat(1000000) * parseFloat(quote.config.param.areaUnitPrice)
        console.log('banPriceRow')
        console.log(length + '--' + width + '--' + perBanCount + '--' + quote.config.param.areaUnitPrice)
        console.log(banPrice)

        //印费
        let processPrice = parseFloat(printCount) * parseFloat(quote.config.param.printUnitPrice) + parseFloat(quote.config.param.start)
        console.log('processPrice')
        console.log(processPrice)

        let processPriceFinal = parseFloat((banPrice + processPrice))
        console.log('processPriceFinal')
        console.log(processPriceFinal)

        let total = Math.round(materialQuantityStepPrice + processPriceFinal)

        if(qty == quantity){
          logic.push("版费 = 尺寸" + length + " * " + width + " * 每版数量" + perBanCount + " / 1000000 * 每平版费" + quote.config.param.areaUnitPrice)
          logic.push("印费 = 印数" + printCount + " * 每印费用" + quote.config.param.printUnitPrice + " + 起点" + quote.config.param.start)
          logic.push("材料 = 尺寸" + length + " * " + width + " / 1000000 * 每平材料费" + materialUnitPrice)
          logic.push("总价 = 版费 + 印费 + 材料")
          this.logic = logic
          postData.amount = total
        }

        quoteList.push({
          quantity: qty,
          materialQuantityStepPrice: materialQuantityStepPrice,
          processQuantityStepPrice: processPriceFinal,
          subtotal: total
        })
        console.log('-------------------------------------------------------------------------------------------------')
      })
      console.log('quoteList')
      console.log(quoteList)
      this.quoteList = quoteList
    },

    submitOrder(){
      const {fileList,postData} = this

      if(fileList.length < 1){
        this.$message.error('请上传设计稿')
        return
      }

      let fileIds = [];
      fileList.forEach(item => {
        fileIds.push(item.id)
      })
      postData.fileIds = fileIds

      const loading = this.$loading({
        lock: true,
        text: '正在创建订单',
        spinner: 'el-icon-loading',
        background: 'rgba(0,0,0,0.7)'
      });

      postQuoteOrder(postData).then(res => {
        loading.close()
        this.$message.success('成功创建订单')
        this.$router.push({
          path: '/submitOrder',
          query: { id : res.id}
        })
      }).catch(err => {
        loading.close()
        this.$message.error('创建订单失败')
      })
    },

    handleUpload: async function(data){
      const formData = new FormData()
      formData.append('file', data.file)
      const res = await uploadFile(formData)
      let fileList = this.fileList
      fileList.push(res.file)
      this.fileList = fileList
      console.log(fileList)
    },

    getStepPriceFromList(value,list){
      let stepPrice = undefined
      list.forEach(i => {
        if(value == i.text){
          stepPrice = i.stepPrice
        }
      })
      return stepPrice
    },
  }
}
